package com.wsdc.encryption;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

public class MainActivity extends AppCompatActivity {
    private String path = "http://api.m.mtime.cn/Showtime/LocationMovies.api?locationId=";

    private String root = "http://www.baidu.com";

    private String s = "http://baike.baidu.com/item/%E4%B9%A0%E5%A4%A7%E5%A4%A7/5338485";

    private String str = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.d("flag", "onCreate: " +"大眼瞪小眼");

        new Thread(new Runnable() {
            @Override
            public void run() {

                InputStream is = null;
                ByteArrayOutputStream bos = null;
                try {
                    Thread.sleep(1000);

                    String temp=URLEncoder.encode("习大大","UTF-8");

                    Log.d("flag", "run: " +temp);

                    //Log.d("flag", "run: " +"开始解码操作");

                    s.replaceAll("习大大",temp);

                    Log.d("flag", "run: " +s);

                    URL url = new URL(s);

                    HttpURLConnection con = (HttpURLConnection) url.openConnection();

                    con.setConnectTimeout(3000);

                    Log.d("flag", "run: " +con.getResponseCode());

                    if(con.getResponseCode() == 200){
                        Log.d("flag", "run: " +99);

                        is = con.getInputStream();

                        bos = new ByteArrayOutputStream();

                        int len = 0;

                        byte[] buf = new byte[1024];

                        while ((len = is.read(buf)) != -1){
                            bos.write(buf,0,len);
                        }

                        str = new String(bos.toByteArray());


                        Log.d("flag", "run: " +str);

                        Log.d("flag", "run: " +(str == null));


                    }


                } catch (MalformedURLException e) {
                    e.printStackTrace();
                    Log.d("flag", "run: " +1);
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.d("flag", "run: " +2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    Log.d("flag", "run: " +3);
                } finally {
                    Log.d("flag", "run: " +"这是为何");
                    if(is != null){
                        try {
                            is.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if(bos != null){
                        try {
                            bos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }
        }).start();
    }
}
